package com.earthchen.hbase.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @author: EarthChen
 * @date: 2018/07/29
 */
@Configuration
@MapperScan("com.earthchen.hbase.dao")
public class PhoenixDataSourceConfig {

    @Value("${datasource.jdbcUrl}")
    private String jdbcUrl;

    @Value("${datasource.driverClassName}")
    private String driverClassName;

    @Value("${datasource.maxPoolSize}")
    private String maxPoolSize;

    @Value("${datasource.minIdle}")
    private String minIdle;

    @Value("${datasource.validationTimeout}")
    private String validationTimeout;

    @Value("${datasource.idleTimeout}")
    private String idleTimeout;

    @Value("${datasource.connectionTestQuery}")
    private String connectionTestQuery;

    @Value("${mybatis.mapperLocations}")
    private String mapperLocations;

    @Autowired
    private MybatisProperties mybatisProperties;


    @Bean("PhoenixDataSource")
    @Primary
    public DataSource phoenixDataSource() throws Exception {
        HikariDataSourceFactory factory = new HikariDataSourceFactory();
        Properties properties = new Properties();
        properties.setProperty("jdbcUrl", jdbcUrl);
        properties.setProperty("driverClassName", driverClassName);
        properties.setProperty("maxPoolSize", maxPoolSize);
        properties.setProperty("minIdle", minIdle);
        properties.setProperty("validationTimeout", validationTimeout);
        properties.setProperty("idleTimeout", idleTimeout);
        properties.setProperty("connectionTestQuery", connectionTestQuery);
        factory.setProperties(properties);
        return factory.getDataSource();
    }

    @Bean("PhoenixSqlSessionFactory")
    @Primary
    public SqlSessionFactory phoenixSqlSessionFactory(@Qualifier("PhoenixDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources(mapperLocations);
        factoryBean.setMapperLocations(resources);
        factoryBean.setConfigLocation(new DefaultResourceLoader().getResource(mybatisProperties.getConfigLocation()));
        return factoryBean.getObject();
    }


}
